The Coronavirus Dashboard: the case of Australia
This Coronavirus dashboard: the case of Australia provides an overview of the 2019 Novel Coronavirus COVID-19 (2019-nCoV) epidemic. This dashboard is built with R using the R Makrdown framework and was adapted from this dashboard by Rami Krispin and this dashboard by Antoine Soetewey.
Code
The code behind my dashboard is available on GitHub.
Data
The input data for this dashboard is the dataset available from the {coronavirus} R package. Make sure to download the development version of the package to have the latest data:
install.packages("devtools")
devtools::install_github("RamiKrispin/coronavirus")
The data and dashboard are refreshed on a daily basis.
The raw data is pulled from the Johns Hopkins University Center for Systems Science and Engineering (JHU CCSE) Coronavirus repository.
Acknowledgement
This dashboard was created using the template provided by Antoine Soetewey. More information about Antoine’s dashboard can be found in this article.
Update
The data is as of Monday April 20, 2020 (UTC) and the dashboard has been updated on Tuesday April 21, 2020 (UTC).
---
title: "Coronavirus in Australia"
author: "Jason Everett"
output:
flexdashboard::flex_dashboard:
orientation: rows
# social: ["facebook", "twitter", "linkedin"]
source_code: embed
vertical_layout: fill
---
```{r setup, include=FALSE}
#------------------ Packages ------------------
library(flexdashboard)
library(tidyverse)
# install.packages("devtools")
# devtools::install_github("RamiKrispin/coronavirus", force = TRUE)
library(coronavirus)
# data(coronavirus)
# update_datasets()
# View(coronavirus)
# max(coronavirus$date)
#----------------------------------------------------
# Pulling the coronvirus data from John Hopkins repo
# https://github.com/CSSEGISandData/COVID-19
#----------------------------------------------------
# Setting functions
# `%>%` <- magrittr::`%>%`
#----------------------------------------------------
#------------ Pulling confirmed cases------------
conf_url <- "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv"
raw_conf <- read.csv(file = conf_url,
stringsAsFactors = FALSE)
lapply(1:ncol(raw_conf), function(i){
if(all(is.na(raw_conf[, i]))){
raw_conf <<- raw_conf[, -i]
return(print(paste("Column", names(raw_conf)[i], "is missing", sep = " ")))
} else {
return(NULL)
}
})
# Transforming the data from wide to long
# Creating new data frame
df_conf <- raw_conf[, 1:4]
for(i in 5:ncol(raw_conf)){
raw_conf[,i] <- as.integer(raw_conf[,i])
# raw_conf[,i] <- ifelse(is.na(raw_conf[, i]), 0 , raw_conf[, i])
print(names(raw_conf)[i])
if(i == 5){
df_conf[[names(raw_conf)[i]]] <- raw_conf[, i]
} else {
df_conf[[names(raw_conf)[i]]] <- raw_conf[, i] - raw_conf[, i - 1]
}
}
df_conf1 <- df_conf %>%
tidyr::pivot_longer(cols = dplyr::starts_with("X"),
names_to = "date_temp",
values_to = "cases_temp")
# Parsing the date
df_conf1$month <- sub("X", "",
strsplit(df_conf1$date_temp, split = "\\.") %>%
purrr::map_chr(~.x[1]) )
df_conf1$day <- strsplit(df_conf1$date_temp, split = "\\.") %>%
purrr::map_chr(~.x[2])
df_conf1$date <- as.Date(paste("2020", df_conf1$month, df_conf1$day, sep = "-"))
# Aggregate the data to daily
df_conf2 <- df_conf1 %>%
dplyr::group_by(Province.State, Country.Region, Lat, Long, date) %>%
dplyr::summarise(cases = sum(cases_temp)) %>%
dplyr::ungroup() %>%
dplyr::mutate(type = "confirmed",
Country.Region = trimws(Country.Region),
Province.State = trimws(Province.State))
head(df_conf2)
tail(df_conf2)
#----------------------------------------------------
# Pulling death cases
death_url <- "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv"
raw_death <- read.csv(file =death_url,
stringsAsFactors = FALSE,
fill =FALSE)
lapply(1:ncol(raw_death), function(i){
if(all(is.na(raw_death[, i]))){
raw_death <<- raw_death[, -i]
return(print(paste("Column", names(raw_death)[i], "is missing", sep = " ")))
} else {
return(NULL)
}
})
# Transforming the data from wide to long
# Creating new data frame
df_death <- raw_death[, 1:4]
for(i in 5:ncol(raw_death)){
print(i)
raw_death[,i] <- as.integer(raw_death[,i])
raw_death[,i] <- ifelse(is.na(raw_death[, i]), 0 , raw_death[, i])
if(i == 5){
df_death[[names(raw_death)[i]]] <- raw_death[, i]
} else {
df_death[[names(raw_death)[i]]] <- raw_death[, i] - raw_death[, i - 1]
}
}
df_death1 <- df_death %>% tidyr::pivot_longer(cols = dplyr::starts_with("X"),
names_to = "date_temp",
values_to = "cases_temp")
# Parsing the date
df_death1$month <- sub("X", "",
strsplit(df_death1$date_temp, split = "\\.") %>%
purrr::map_chr(~.x[1]) )
df_death1$day <- strsplit(df_death1$date_temp, split = "\\.") %>%
purrr::map_chr(~.x[2])
df_death1$date <- as.Date(paste("2020", df_death1$month, df_death1$day, sep = "-"))
# Aggregate the data to daily
df_death2 <- df_death1 %>%
dplyr::group_by(Province.State, Country.Region, Lat, Long, date) %>%
dplyr::summarise(cases = sum(cases_temp)) %>%
dplyr::ungroup() %>%
dplyr::mutate(type = "death",
Country.Region = trimws(Country.Region),
Province.State = trimws(Province.State))
#---------------- Aggregate all cases ----------------
#
coronavirus <- dplyr::bind_rows(df_conf2, df_death2) %>%
as.data.frame()
#------------------ Parameters ------------------
# Set colors
# https://www.w3.org/TR/css-color-3/#svg-color
confirmed_color <- "purple"
active_color <- "#1f77b4"
recovered_color <- "forestgreen"
death_color <- "red"
#------------------ Data ------------------
df <- coronavirus %>%
# dplyr::filter(date == max(date)) %>%
dplyr::filter(Country.Region == "Australia") %>%
dplyr::group_by(Country.Region, type) %>%
dplyr::summarise(total = sum(cases)) %>%
tidyr::pivot_wider(
names_from = type,
values_from = total
) %>%
# dplyr::mutate(unrecovered = confirmed - ifelse(is.na(recovered), 0, recovered) - ifelse(is.na(death), 0, death)) %>%
dplyr::mutate(unrecovered = confirmed - ifelse(is.na(death), 0, death)) %>%
dplyr::arrange(-confirmed) %>%
dplyr::ungroup() %>%
dplyr::mutate(country = dplyr::if_else(Country.Region == "United Arab Emirates", "UAE", Country.Region)) %>%
dplyr::mutate(country = dplyr::if_else(country == "Mainland China", "China", country)) %>%
dplyr::mutate(country = dplyr::if_else(country == "North Macedonia", "N.Macedonia", country)) %>%
dplyr::mutate(country = dplyr::if_else(country == "New Zealand", "NewZealand", country)) %>%
dplyr::mutate(country = trimws(country)) %>%
dplyr::mutate(country = factor(country, levels = country))
df_daily <- coronavirus %>%
dplyr::filter(Country.Region == "Australia") %>%
dplyr::group_by(date, type) %>%
dplyr::summarise(total = sum(cases, na.rm = TRUE)) %>%
tidyr::pivot_wider(
names_from = type,
values_from = total
) %>%
dplyr::arrange(date) %>%
dplyr::ungroup() %>%
#dplyr::mutate(active = confirmed - death - recovered) %>%
dplyr::mutate(active = confirmed - death) %>%
dplyr::mutate(
confirmed_cum = cumsum(confirmed),
death_cum = cumsum(death),
# recovered_cum = cumsum(recovered),
active_cum = cumsum(active)
)
df1 <- coronavirus %>% dplyr::filter(date == max(date))
```
Summary
=======================================================================
Row {data-width=400}
-----------------------------------------------------------------------
### confirmed {.value-box}
```{r}
valueBox(
value = paste(format(sum(df$confirmed), big.mark = ","), "", sep = " "),
caption = "Total confirmed cases",
icon = "fas fa-user-md",
color = confirmed_color
)
```
### death {.value-box}
```{r}
valueBox(
value = paste(format(sum(df$death, na.rm = TRUE), big.mark = ","), " (",
round(100 * sum(df$death, na.rm = TRUE) / sum(df$confirmed), 2),
"%)",
sep = ""
),
caption = "Death cases (death rate)",
icon = "fas fa-heart-broken",
color = death_color
)
```
Row
-----------------------------------------------------------------------
### **Daily cumulative cases by type** (Australia only)
```{r}
plotly::plot_ly(data = df_daily) %>%
plotly::add_trace(
x = ~date,
# y = ~active_cum,
y = ~confirmed_cum,
type = "scatter",
mode = "lines+markers",
# name = "Active",
name = "Confirmed",
line = list(color = active_color),
marker = list(color = active_color)
) %>%
plotly::add_trace(
x = ~date,
y = ~death_cum,
type = "scatter",
mode = "lines+markers",
name = "Death",
line = list(color = death_color),
marker = list(color = death_color)
) %>%
plotly::add_annotations(
x = as.Date(df_daily$date[df_daily$confirmed>0][1]),
y = 1,
text = paste("First case"),
xref = "x",
yref = "y",
arrowhead = 5,
arrowhead = 3,
arrowsize = 1,
showarrow = TRUE,
ax = -10,
ay = -90
) %>%
plotly::add_annotations(
x = as.Date(df_daily$date[df_daily$death>0][1]),
y = 3,
text = paste("First death"),
xref = "x",
yref = "y",
arrowhead = 5,
arrowhead = 3,
arrowsize = 1,
showarrow = TRUE,
ax = -50,
ay = -50
) %>%
plotly::add_annotations(
x = as.Date("2020-03-18"),
y = 681,
text = paste(
"Ruby Princess\ndocks in Sydney "
),
xref = "x",
yref = "y",
arrowhead = 5,
arrowhead = 3,
arrowsize = 1,
showarrow = TRUE,
ax = -80,
ay = 0
) %>%
plotly::add_annotations(
x = as.Date("2020-03-22"),
y = 1682,
text = paste(
"Level 1 containment measures "
),
xref = "x",
yref = "y",
arrowhead = 5,
arrowhead = 3,
arrowsize = 1,
showarrow = TRUE,
ax = -150,
ay = 0
) %>%
plotly::add_annotations(
x = as.Date("2020-03-24"),
y = 2364,
text = paste(
"Level 2 containment measures "
),
xref = "x",
yref = "y",
arrowhead = 5,
arrowhead = 3,
arrowsize = 1,
showarrow = TRUE,
ax = -150,
ay = 0
) %>%
plotly::layout(
title = "",
yaxis = list(title = "Cumulative number of cases"),
xaxis = list(title = "Date"),
legend = list(x = 0.1, y = 0.9),
hovermode = "compare"
)
```
Comparison
=======================================================================
Column {data-width=400}
-------------------------------------
### **Daily new cases**
```{r}
daily_confirmed <- coronavirus %>%
dplyr::filter(type == "confirmed") %>%
dplyr::filter(date >= "2020-02-29") %>%
dplyr::mutate(country = Country.Region) %>%
dplyr::mutate(country = dplyr::if_else(country == "New Zealand", "NewZealand", country)) %>%
dplyr::filter(Country.Region == "Australia" |
Country.Region == "New Zealand" |
Country.Region == "China"
# |
# Country.Region == "US" |
# Country.Region == "Italy"
) %>%
dplyr::group_by(date, country) %>%
dplyr::summarise(total = sum(cases)) %>%
dplyr::ungroup() %>%
tidyr::pivot_wider(names_from = country, values_from = total)
#----------------------------------------
# Plotting the data
daily_confirmed %>%
plotly::plot_ly() %>%
plotly::add_trace(
x = ~date,
y = ~Australia,
type = "scatter",
mode = "lines+markers",
name = "Australia"
) %>%
plotly::add_trace(
x = ~date,
y = ~NewZealand,
type = "scatter",
mode = "lines+markers",
name = "New Zealand"
) %>%
plotly::add_trace(
x = ~date,
y = ~China,
type = "scatter",
mode = "lines+markers",
name = "China"
) %>%
# plotly::add_trace(
# x = ~date,
# y = ~US,
# type = "scatter",
# mode = "lines+markers",
# name = "US"
# ) %>%
# plotly::add_trace(
# x = ~date,
# y = ~Italy,
# type = "scatter",
# mode = "lines+markers",
# name = "Italy"
# ) %>%
plotly::layout(
title = "",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Number of new cases"),
xaxis = list(title = "Date"),
# paper_bgcolor = "black",
# plot_bgcolor = "black",
# font = list(color = 'white'),
hovermode = "compare",
margin = list(
# l = 60,
# r = 40,
b = 10,
t = 10,
pad = 2
)
)
```
### **Cases distribution by type**
```{r daily_summary}
df_region <- coronavirus %>%
# dplyr::filter(date == max(date)) %>%
dplyr::filter(Country.Region == "Australia" |
Country.Region == "New Zealand" |
Country.Region == "China" #|
# Country.Region == "US" |
# Country.Region == "Italy"
) %>%
dplyr::group_by(Country.Region, type) %>%
dplyr::summarise(total = sum(cases)) %>%
tidyr::pivot_wider(
names_from = type,
values_from = total
) %>%
# dplyr::mutate(unrecovered = confirmed - ifelse(is.na(recovered), 0, recovered) - ifelse(is.na(death), 0, death)) %>%
dplyr::mutate(unrecovered = confirmed - ifelse(is.na(death), 0, death)) %>%
dplyr::arrange(confirmed) %>%
dplyr::ungroup() %>%
dplyr::mutate(country = dplyr::if_else(Country.Region == "United Arab Emirates", "UAE", Country.Region)) %>%
dplyr::mutate(country = dplyr::if_else(country == "Mainland China", "China", country)) %>%
dplyr::mutate(country = dplyr::if_else(country == "North Macedonia", "N.Macedonia", country)) %>%
dplyr::mutate(country = trimws(country)) %>%
dplyr::mutate(country = factor(country, levels = country))
plotly::plot_ly(
data = df_region,
x = ~country,
# y = ~unrecovered,
y = ~ confirmed,
# text = ~ confirmed,
# textposition = 'auto',
type = "bar",
name = "Confirmed",
marker = list(color = active_color)
) %>%
plotly::add_trace(
y = ~death,
# text = ~ death,
# textposition = 'auto',
name = "Death",
marker = list(color = death_color)
) %>%
plotly::layout(
barmode = "stack",
yaxis = list(title = "Total cases"),
xaxis = list(title = ""),
hovermode = "compare",
margin = list(
# l = 60,
# r = 40,
b = 10,
t = 10,
pad = 2
)
)
```
Map
=======================================================================
### **World map of cases** (*use + and - icons to zoom in/out*)
```{r}
# map tab added by Art Steinmetz
library(leaflet)
library(leafpop)
library(purrr)
cv_data_for_plot <- coronavirus %>%
# dplyr::filter(Country.Region == "m") %>%
dplyr::filter(cases > 0) %>%
dplyr::group_by(Country.Region, Province.State, Lat, Long, type) %>%
dplyr::summarise(cases = sum(cases)) %>%
dplyr::mutate(log_cases = 2 * log(cases)) %>%
dplyr::ungroup()
cv_data_for_plot.split <- cv_data_for_plot %>% split(cv_data_for_plot$type)
pal <- colorFactor(c("orange", "red", "green"), domain = c("confirmed", "death", "recovered"))
map_object <- leaflet() %>% addProviderTiles(providers$Stamen.Toner)
names(cv_data_for_plot.split) %>%
purrr::walk(function(df) {
map_object <<- map_object %>%
addCircleMarkers(
data = cv_data_for_plot.split[[df]],
lng = ~Long, lat = ~Lat,
# label=~as.character(cases),
color = ~ pal(type),
stroke = FALSE,
fillOpacity = 0.8,
radius = ~log_cases,
popup = leafpop::popupTable(cv_data_for_plot.split[[df]],
feature.id = FALSE,
row.numbers = FALSE,
zcol = c("type", "cases", "Country.Region", "Province.State")
),
group = df,
# clusterOptions = markerClusterOptions(removeOutsideVisibleBounds = F),
labelOptions = labelOptions(
noHide = F,
direction = "auto"
)
)
})
map_object %>%
addLayersControl(
overlayGroups = names(cv_data_for_plot.split),
options = layersControlOptions(collapsed = FALSE)
)
```
About
=======================================================================
**The Coronavirus Dashboard: the case of Australia**
This Coronavirus dashboard: the case of Australia provides an overview of the 2019 Novel Coronavirus COVID-19 (2019-nCoV) epidemic. This dashboard is built with R using the R Makrdown framework and was adapted from this [dashboard](https://ramikrispin.github.io/coronavirus_dashboard/){target="_blank"} by Rami Krispin and this [dashboard](https://www.antoinesoetewey.com/files/coronavirus-dashboard.html){target="_blank"} by Antoine Soetewey.
**Code**
The code behind my dashboard is available on [GitHub](https://github.com/jaseeverett/coronavirus_dashboard){target="_blank"}.
**Data**
The input data for this dashboard is the dataset available from the [`{coronavirus}`](https://github.com/RamiKrispin/coronavirus){target="_blank"} R package. Make sure to download the development version of the package to have the latest data:
```
install.packages("devtools")
devtools::install_github("RamiKrispin/coronavirus")
```
The data and dashboard are refreshed on a daily basis.
The raw data is pulled from the Johns Hopkins University Center for Systems Science and Engineering (JHU CCSE) Coronavirus [repository](https://github.com/RamiKrispin/coronavirus-csv){target="_blank"}.
**Acknowledgement**
This dashboard was created using the template provided by Antoine Soetewey. More information about Antoine's dashboard can be found in this [article](https://www.statsandr.com/blog/how-to-create-a-simple-coronavirus-dashboard-specific-to-your-country-in-r/).
**Update**
The data is as of `r format(max(coronavirus$date), "%A %B %d, %Y")` (UTC) and the dashboard has been updated on `r format(Sys.time() - (11*3600), "%A %B %d, %Y")` (UTC).